package br.com.onlinebidding.dao;

import java.util.List;
import org.hibernate.Session;
import br.com.onlinebidding.config.HibernateUtil;
import br.com.onlinebidding.entity.User;

public class UserDAO {

	public User getUser(int id) {

		Session sessao = null;
		sessao = HibernateUtil.getSessionFactory().openSession();

		return (User) sessao.load(User.class, id);
	}

	public static boolean insert(User u) {
		Session sessao = null;
		System.out.print(u.getEmail()+u.getId()+u.getName()+u.getNumberRegistration());

		try {
			sessao = HibernateUtil.getSessionFactory().openSession();
			sessao.beginTransaction();
			sessao.save(u);
			sessao.getTransaction().commit();
			sessao.close();
			return true;
		} catch (Exception e) {
			System.out.print("Erro UserDao.insert():" + e.getMessage());
		} finally {
			if (sessao.isConnected()) {
				sessao.close();

			}

		}
		return false;
	}

	@SuppressWarnings("unchecked")
	public static List<User> getAll() {
		String comando = "from User";
		List<User> lista = null;
		Session sessao = null;

		try {
			sessao = HibernateUtil.getSessionFactory().openSession();
			lista = (List<User>) sessao.createQuery(comando).list();
		} catch (Exception ex) {
			System.out.println("Erro UserDao.getAll(): " + ex.getMessage());
		} finally {
			if (sessao.isConnected()) {
				sessao.close();
			}
		}
		return lista;
	}

	@SuppressWarnings("unchecked")
	public static List<User> getById(Integer id) {
		String comando = " from User where id = " + id;
		Session sessao = null;
		List<User> us = null;
		try {
			sessao = HibernateUtil.getSessionFactory().openSession();
			us = (List<User>) sessao.createQuery(comando).list();
		} catch (Exception ex) {
			System.out.println("Erro UserDao.getById():" + ex.getMessage());
		} finally {
			if (sessao.isConnected()) {
				sessao.close();
			}
		}
		return us;
	}

	public static User getByIdRetObjeto(Integer id) {

		Session sessao = null;
		User u = null;
		try {
			sessao = HibernateUtil.getSessionFactory().openSession();
			u = (User) sessao.get(User.class, id);

		} catch (Exception ex) {
			System.out.println("Erro UserDao.getByIdRetObjeto():"
					+ ex.getMessage());
		} finally {
			if (sessao.isConnected()) {
				sessao.close();
			}
		}
		return u;
	}

	public static boolean update(User u) {
		Session sessao = null;

		try {
			sessao = HibernateUtil.getSessionFactory().openSession();
			sessao.beginTransaction();
			sessao.update(u);
			sessao.getTransaction().commit();
			sessao.close();
			return true;

		} catch (Exception ex) {
			System.out.println("Erro UserDao.update();" + ex.getMessage());

		} finally {
			if (sessao.isConnected()) {
				sessao.close();
			}
		}
		return false;
	}

	public static boolean delete(User u) {
		Session sessao = null;

		try {
			sessao = HibernateUtil.getSessionFactory().openSession();
			sessao.beginTransaction();
			sessao.delete(u);
			sessao.getTransaction().commit();
			sessao.close();
			return true;

		} catch (Exception ex) {
			System.out.println("Erro UserDao.delete();" + ex.getMessage());

		} finally {
			if (sessao.isConnected()) {
				sessao.close();
			}
		}
		return false;
	}

}
